package output;

import instance.InstanceEj1;

import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import problem.ej2.Link;

public class Problem1Output implements  Output {
	
	 
		private static String outputFile;
		private LinkedList<Integer> solutions;
		
		public Problem1Output(LinkedList<Integer> solutions2) {
			this.solutions = solutions2;
		}
		public Problem1Output(String nombArch){
			
		  outputFile =nombArch;
		}
		public void writeTestRend(LinkedList<String> strings_salida) throws Exception {
			/*||CUIDADO||:Usar después de generar el tipo Problem3Output con el constructor que cambia el outputfile*/
			BufferedWriter bw = new BufferedWriter(new PrintWriter(
					Problem1Output.outputFile));
			
			Iterator<String> it_res = strings_salida.iterator();
			while(it_res.hasNext()){
				String st = it_res.next();
				bw.write(st);
				if(it_res.hasNext()) bw.newLine();
			}
			bw.close();
		}
		
		@Override
		public void writeFile(String outputFile) throws Exception {
			// TODO Auto-generated method stub

			Problem1Output.outputFile = outputFile;

			BufferedWriter bufferedWriter = new BufferedWriter(new PrintWriter(
					Problem1Output.outputFile));
			
			for (int i = 0; i < solutions.size(); i++) {
				String linea = " ";
				// Por cada soluci�n.
				Integer solution =  solutions.get(i);

				printSolution(bufferedWriter, solution,linea);
			}

			bufferedWriter.close();
		}
		
		// Imprime una solucion en el archivo de salida.
		private void printSolution(BufferedWriter bw,Integer solution, String linea) throws Exception {
			if (solution== null){
				linea = "";
			}else{
				
				linea=String.valueOf(solution);
			}
			bw.write(linea);
	        bw.newLine();	
			}
		
		public static void deListaAArchivo(LinkedList<InstanceEj1 > lista, String nombArch)throws Exception{
			BufferedWriter bw = new BufferedWriter(new PrintWriter(nombArch));
			
			Iterator<InstanceEj1 > it = lista.iterator();
			while(it.hasNext()){
				InstanceEj1 inst = it.next();
				
				String capacidad_y_energia = "";
				capacidad_y_energia = capacidad_y_energia.concat(String.valueOf(inst.capacidad) + " " + String.valueOf(inst.energia));
				bw.write(capacidad_y_energia);
				bw.newLine();
		        String vector = "";
		        Iterator<Integer> it_pisos = (inst.pers_x_piso).iterator();
				while(it_pisos.hasNext()){
					Integer i = it_pisos.next();
					vector = vector.concat(String.valueOf(i));
					if(it_pisos.hasNext())vector = vector.concat(" ");
					
				}
				bw.write(vector);
				if(it.hasNext())bw.newLine();	        
			}    

		    bw.close();
		}

}
